package visual;

import java.awt.Frame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.util.Collection;

import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import modelo.Funcionario;
import negocio.GerenciadorFuncionario;

public class FrmPesquisaFuncionario extends FormularioBase {

	private JPanel jPanel = null;
	private BarraDeControle barraDeControle = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField nomeFuncionarioTextField = null;
	private JLabel jLabel1 = null;
	private JTextField matriculaTextField = null;
	private JLabel jLabel2 = null;
	private JButton pesquisarButton = null;
	private JScrollPane jScrollPane = null;
	private JTable funcionariosTable = null;
	public FrmPesquisaFuncionario(Frame owner) {
		super(owner);
		initialize();
		// TODO Auto-generated constructor stub
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(783, 328));
        this.setTitle("Pesquisa de funcionário");
        this.setContentPane(getJPanel());
        this.addWindowListener(new java.awt.event.WindowAdapter() {
        	public void windowOpened(java.awt.event.WindowEvent e) {
        		System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()
        		getBarraDeControle().ativaBotoes(true, false, false, false, false, false);
        		getBarraDeControle().removerBotoes(false, true, false, false, false, false);
        		pesquisar();
        		
        	}
        });
        getBarraDeControle().setFormulario(this);
			
	}
	public void pesquisar(){
		getFuncionariosTable().setModel(new MyTableModel(new String[][]{}, 
				new String[]{"Registro","Nome", "Matricula"}));
		DefaultTableModel model = (MyTableModel) funcionariosTable.getModel() ;
		
		GerenciadorFuncionario gerenciadorFuncionario = new GerenciadorFuncionario();
		Collection<Funcionario> funcionarios = gerenciadorFuncionario.listarFuncionarios();
		
		for(Funcionario f: funcionarios){
			model.addRow(new String[]{ String.valueOf(f.getId()), f.getNome(), f.getMatricula() });
		}	
	}
	@Override
	public void carregaFormulario() {
		// TODO Auto-generated method stub

	}

	@Override
	public void salvaFormulario() {
		// TODO Auto-generated method stub

	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Pesquisa de funcionários");
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getBarraDeControle(), BorderLayout.SOUTH);
			jPanel.add(jLabel2, BorderLayout.NORTH);
			jPanel.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes barraDeControle	
	 * 	
	 * @return visual.BarraDeControle	
	 */
	private BarraDeControle getBarraDeControle() {
		if (barraDeControle == null) {
			barraDeControle = new BarraDeControle();
		}
		return barraDeControle;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(114, 49, 71, 14));
			jLabel1.setText("Matricula:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(31, 21, 151, 14));
			jLabel.setText("Nome do funcionário:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(jLabel, null);
			jPanel1.add(getNomeFuncionarioTextField(), null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(getMatriculaTextField(), null);
			jPanel1.add(getPesquisarButton(), null);
			jPanel1.add(getJScrollPane(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes nomeFuncionarioTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNomeFuncionarioTextField() {
		if (nomeFuncionarioTextField == null) {
			nomeFuncionarioTextField = new JTextField();
			nomeFuncionarioTextField.setBounds(new Rectangle(193, 18, 453, 18));
		}
		return nomeFuncionarioTextField;
	}

	/**
	 * This method initializes matriculaTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMatriculaTextField() {
		if (matriculaTextField == null) {
			matriculaTextField = new JTextField();
			matriculaTextField.setBounds(new Rectangle(193, 45, 243, 18));
		}
		return matriculaTextField;
	}

	/**
	 * This method initializes pesquisarButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPesquisarButton() {
		if (pesquisarButton == null) {
			pesquisarButton = new JButton();
			pesquisarButton.setBounds(new Rectangle(660, 15, 105, 24));
			pesquisarButton.setText("Pesquisar");
		}
		return pesquisarButton;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(15, 75, 751, 166));
			jScrollPane.setViewportView(getFuncionariosTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes funcionariosTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getFuncionariosTable() {
		if (funcionariosTable == null) {
			// TODO getFuncionariosTable()
			funcionariosTable = new JTable();
			//funcionariosTable.getModel().
			funcionariosTable.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// TODO Auto-generated Event stub mouseClicked()
					if(e.getClickCount() == 2	){
						FrmCadastroFuncionario frm = new FrmCadastroFuncionario();
						frm.setId(Integer.parseInt( 
								getFuncionariosTable().getValueAt(
										getFuncionariosTable().getSelectedRow(), 0).toString()));
						frm.setVisible(true);
						pesquisar();
					}
				}
			});
		}
		return funcionariosTable;
	}

	@Override
	public void apagarFormulario() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configuraEdicao() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configuraNovo() {
		// TODO Auto-generated method stub
		
	}

}  //  @jve:decl-index=0:visual-constraint="11,11"
